<template>
  <div class="app-container">
    <div class="spaceline" />
    <el-form
      ref="form"
      class="common-form"
      :model="form"
      label-width="120px"
    >
      <el-form-item label="token">
        <el-input v-model="form.qianfan_token" class="middle" />
        <span class="smalltip">
          <i class="el-icon-info" />
          请填写APP后台配置token，如：12cbd39c89e23
        </span>
      </el-form-item>
      <el-form-item label="API域名">
        <el-input v-model="form.qianfan_domain" class="middle" />
        <span class="smalltip">
          <i class="el-icon-info" />
          请填写完整客户端域名，结尾不要加"/"，如：https://www.bfrsks.com
        </span>
      </el-form-item>
      <el-form-item label="" style="margin-top:36px;">
        <el-button type="primary" @click="onSubmit('form')">保存</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import { setConfig } from '@/api/configuration'

export default {
  data() {
    return {
      form: {
        qianfan_token: '',
        qianfan_domain: ''
      }
    }
  },
  created() {
    this.fetchInfo()
  },
  methods: {
    fetchInfo() {
      const param = {}
      setConfig(param, 'get')
        .then(response => {
          const {
            qianfan_token,
            qianfan_domain
          } = { ...response.data }
          this.form = {
            qianfan_token,
            qianfan_domain
          }
        })
        .catch(() => {})
    },
    onSubmit(formName) {
      const insertData = { ...this.form }
      this.$refs[formName].validate(valid => {
        if (valid) {
          setConfig(insertData)
            .then(response => {
              this.$message.success(response.message)
              return true
            })
            .catch(() => {})
        } else {
          return false
        }
      })
    }
  }
}
</script>

